package com.dgut.service.serviceImpl;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.common.*;
import com.dgut.mapper.CompanyConversionMapper;
import com.dgut.mapper.OrderFlagMapper;
import com.dgut.pojo.CompanyConversion;
import com.dgut.pojo.OrderFlag;
import com.dgut.service.CompanyConversionService;
import com.dgut.service.NoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings({"all"})
@Service
public class CompanyConversionServiceImpl implements CompanyConversionService {
    @Resource
    private CompanyConversionMapper companyConversionMapper;
    @Resource
    private OrderFlagMapper orderFlagMapper;
    @Resource
    private NoticeService noticeService;

    /**
     * start-zwb
     */
    //获取公司变更信息
    public Object getCompanyConversionService(Integer id)
    {
        return companyConversionMapper.selectById(id);
    }
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.BUYER)//角色认证
    public Object CompanyConversion(CompanyConversion companyConversion){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        //1.获取购买者的id,buyerId
        Object loginId = StpUtil.getLoginId();
        int buyerId = Integer.parseInt(loginId.toString());
        //2.把公司变更基本信息插入公司表
        int insert1 = companyConversionMapper.insert(companyConversion);
        if (insert1 >= 0){
            resultMap.put("result1",true);
            resultMap.put("message1","提交成功！请等待管理员派单！");
        } else {
            resultMap.put("result1",false);
            resultMap.put("message1","提交失败！");
        }
        //3.生成订单
        OrderFlag orderFlag = new OrderFlag(buyerId,null, OrderType.COMPANY_CONVERSION, companyConversion.getId(),
                new Date(),null,new Date(),priceOfOrder.COMPANY_CONVERSION, StateForOrder.WAITING_FOR_ASSIGNED,
                OnOrOff.OFF, ExceptionIdentificationForOrder.NORMAL);
        //4.把订单插入订单表
        int insert2 = orderFlagMapper.insert(orderFlag);
        noticeService.orderChangedStateNotice(orderFlag);
        if (insert2 >= 0){
            resultMap.put("result2",true);
            resultMap.put("message2","订单生成成功！");
        } else {
            resultMap.put("result2",false);
            resultMap.put("message2","订单生成失败！");
        }
        return resultMap;
    }

    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */
    /**
     * end-hwf
     */
}
